package com.item.service;

import com.item.dao.OrderMapper;
import com.item.dao.UserMapper;
import com.item.dao.model.Order;
import com.item.dao.model.OrderExample;
import com.item.dao.model.User;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;

    private static final Logger logger = Logger.getLogger(OrderService.class);

    public int countByExample(OrderExample example) {
        return this.orderMapper.countByExample(example);
    }

    public Order selectByPrimaryKey(Integer id) {
        return this.orderMapper.selectByPrimaryKey(id);
    }

    public List<Order> selectByExample(OrderExample example) {
        return this.orderMapper.selectByExample(example);
    }

    public int deleteByPrimaryKey(Integer id) {
        return this.orderMapper.deleteByPrimaryKey(id);
    }

    public int updateByPrimaryKeySelective(Order record) {
        return this.orderMapper.updateByPrimaryKeySelective(record);
    }

    public int updateByPrimaryKey(Order record) {
        return this.orderMapper.updateByPrimaryKey(record);
    }

    public int deleteByExample(OrderExample example) {
        return this.orderMapper.deleteByExample(example);
    }

    public int updateByExampleSelective(Order record, OrderExample example) {
        return this.orderMapper.updateByExampleSelective(record, example);
    }

    public int updateByExample(Order record, OrderExample example) {
        return this.orderMapper.updateByExample(record, example);
    }

    public int insert(Order record) {
        return this.orderMapper.insert(record);
    }

    public int insertSelective(Order record) {
        return this.orderMapper.insertSelective(record);
    }
    
    /**
	 * 修改订单状态
	 * @param status
	 * @param id
	 * @param userid
	 * @return
	 */
    public int setOrderStatus(Integer status, Integer id, String userid) {
		int rows = 0;
		
		Order order = new Order();
		order.setId(id);
		order.setStatus(status); //1取消，0正常，2已完成
		
		OrderExample orderExample = new OrderExample();
		OrderExample.Criteria criteria = orderExample.createCriteria();
		criteria.andIdEqualTo(id);
		if(StringUtils.isNotBlank(userid))
			criteria.andUseridEqualTo(userid);
		if(status == 1)
			criteria.andStatusEqualTo(0); //只有为确认的订单才能取消
		rows = this.orderMapper.updateByExampleSelective(order, orderExample);
		
		return rows;
	}
    
    public int createNewOrder(String userid) {
    	int res =  -1;
    	
    	User user = this.userMapper.selectByPrimaryKey(userid);
		Order order = new Order();
		order.setUserid(userid);
		order.setCreateTime(new Date());
		order.setAddress(user.getAddress());
		order.setPhone(user.getPhone());
		order.setUsername(user.getUsername());
		order.setsName(user.getsName());
		order.setSid(user.getSid());
		order.setStatus(0);
		int rows = this.orderMapper.insertSelective(order);
		
		if(rows > 0) {
			res = order.getId();
		}
 
    	return res;
    }
}